package com.yun.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.yun.reggie.common.BaseContext;
import com.yun.reggie.common.R;
import com.yun.reggie.dto.OrdersDto;
import com.yun.reggie.entity.OrderDetail;
import com.yun.reggie.entity.Orders;
import com.yun.reggie.service.OrderDetailService;
import com.yun.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiao
 */

@RestController
@RequestMapping("order")
@Slf4j
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * {remark: "", payMethod: 1, addressBookId: "1637811250282889217"}
     * 其他订单信息可以通过用户id去查
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> add(@RequestBody Orders orders){
        ordersService.saveOrder(orders);
        return R.success("订单提交成功！");
    }

    /**
     * 后台管理系统的分页展示
     * http://localhost:8080/order/page?page=1&pageSize=10&number=16669
     * &beginTime=2023-03-15%2000%3A00%3A00&endTime=2023-04-29%2023%3A59%3A59
     * @param page
     * @param pageSize
     * @param number 订单号
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page<Orders>> page(int page,int pageSize,String number,String beginTime,String endTime){
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasLength(number),Orders::getNumber,number)
                .gt(StringUtils.hasLength(beginTime),Orders::getOrderTime,beginTime)
                .lt(StringUtils.hasLength(endTime),Orders::getOrderTime,endTime)
                .orderByDesc(Orders::getOrderTime);
        ordersService.page(pageInfo,wrapper);
        return R.success(pageInfo);
    }

    /**
     * 移动端的分页展示
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> userPage(int page, int pageSize){
        Long userId = BaseContext.getCurrentId();
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>(page,pageSize);
        //分页查询订单基本信息
        LambdaQueryWrapper<Orders> ordersWrapper = new LambdaQueryWrapper<>();
        ordersWrapper.eq(userId != null,Orders::getUserId,userId)
                .orderByDesc(Orders::getOrderTime);
        ordersService.page(ordersPage,ordersWrapper);
        //把分页结果中 除records属性 的其他属性移植到ordersDtoPage
        BeanUtils.copyProperties(ordersPage,ordersDtoPage,"records");

        //需要把List<Orders> 改成成 List<OrdersDto>
        List<Orders> ordersList = ordersPage.getRecords();
        List<OrdersDto> ordersDtoList = new ArrayList<>();
        //根据订单集合 查询订单详细信息
        for (Orders orders:ordersList) {
            OrdersDto ordersDto = new OrdersDto();
            //将Orders 信息 移植到 OrdersDto
            BeanUtils.copyProperties(orders,ordersDto);
            //设置OrdersDto的 List<OrderDetail>
            List<OrderDetail> orderDetailList = getOrderDetailListByOrderId(orders.getId());
            ordersDto.setOrderDetails(orderDetailList);
            ordersDtoList.add(ordersDto);
        }
        ordersDtoPage.setRecords(ordersDtoList);
        return R.success(ordersDtoPage);
    }

    /**
     * 工具方法，根据订单id获取订单详情集合。用于userPage()
     * @param ordersId
     * @return
     */
    public List<OrderDetail> getOrderDetailListByOrderId(Long ordersId){
        LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ordersId != null,OrderDetail::getOrderId,ordersId);
        List<OrderDetail> orderDetailList = orderDetailService.list(wrapper);
        return orderDetailList;
    }
}
